package com.edgclub.edg.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzTopicReplyTopicActivity;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DensityUtils;

import java.util.ArrayList;

import me.nereo.multi_image_selector.bean.Image;

/**
 */
public class BBCommentImageAdapter extends GzBaseAdapter {
    private Context mContext;

    private ArrayList<Image> selectPathList;

    private GlideUtils glideUtils;
    private int radius;

    private CloseclickListener closeclickListener;

    public BBCommentImageAdapter(Context mContext) {
        this.mContext = mContext;
        glideUtils = new GlideUtils();
        radius = DensityUtils.dip2px(mContext,5);
    }

    public ArrayList<Image> getSelectPathList() {
        return selectPathList;
    }

    public void setSelectPathList(ArrayList<Image> selectPathList) {
        this.selectPathList = selectPathList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bb_select_image,viewGroup,false);
        ViewHolder vh = new ViewHolder(view,mListener,mLongClickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.close.setVisibility(View.VISIBLE);
        if(position == selectPathList.size()){
            viewHolder.image.setImageResource(R.drawable.add);
            viewHolder.close.setVisibility(View.GONE);
        }else{
//            glideUtils.loadImageSimpleTarget(mContext,selectPathList.get(position).path, new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
//                    selectPathList.get(position).setWidth(bitmap.getWidth());
//                    selectPathList.get(position).setHeight(bitmap.getHeight());
//                }
//
//            });
            glideUtils.Glidde_loadRoudImage(mContext, selectPathList.get(position).path, viewHolder.image,radius);
        }

        viewHolder.close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(closeclickListener!=null)closeclickListener.close(position);
            }
        });
        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(closeclickListener!=null)closeclickListener.addImage(position);
            }
        });
    }


    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public int getItemCount() {
        if(selectPathList.size()< GzTopicReplyTopicActivity.maxImageCount){
            return selectPathList.size()+1;
        }
        return selectPathList.size();
    }

    public CloseclickListener getCloseclickListener() {
        return closeclickListener;
    }

    public void setCloseclickListener(CloseclickListener closeclickListener) {
        this.closeclickListener = closeclickListener;
    }

    public interface CloseclickListener {
        public void addImage(int position);

        public void close(int position);
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        public ImageView image,close;

        public ViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            image = (ImageView) view.findViewById(R.id.image);
            close = (ImageView) view.findViewById(R.id.image_close);
        }
    }

}
